package com.restart.game.common.util.filter;

import com.restart.game.entity.Conf;
import com.restart.game.mapper.ConfMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ReStartLin
 * @data 2018/12/24 15:18
 * @classDesc: 功能描述:
 */
@Component
public class NoticeFilterConf {
    private static final Logger logger = LoggerFactory.getLogger(NoticeFilterConf.class);
    private static NoticeFilterConf instance = null;

    private Map<String, Integer> titleMap = new HashMap<>();
    private String[] ps = null;
    private String[] frists = null;
    private String[] lasts = null;


    @Autowired
    private ConfMapper confMapper;

    private NoticeFilterConf() {
    }

    public void upData() {
        List<Conf> confs = confMapper.selectAll();
        Conf conf = confs.get(0);
        String[] titles = conf.getTitleCf().split("\\|");
        for (String title : titles) {
            this.titleMap.put(title.trim(), 1);
        }
        ps = conf.getPCf().split("\\|");
        frists = conf.getFirstCf().split("\\|");
        lasts = conf.getLastCf().split("\\|");
        logger.info("数据更新成功");
        logger.info(this.toString());
    }



    public static NoticeFilterConf getInstance() {
        return instance;
    }

    @PostConstruct
    public void init() {
        logger.info("initFilter");
        instance = this;
        instance.confMapper = this.confMapper;
        if (confMapper != null) {
            upData();
        } else {
            logger.error("mapper为null");
        }
    }

    @Override
    public String toString() {
        return "NoticeFilterConf{" +
                "titleMap=" + titleMap +
                ", ps=" + Arrays.toString(ps) +
                ", frists=" + Arrays.toString(frists) +
                ", lasts=" + Arrays.toString(lasts) +
                '}';
    }

    public Map<String, Integer> getTitleMap() {
        return titleMap;
    }

    public String[] getPs() {
        return ps;
    }

    public String[] getFrists() {
        return frists;
    }

    public String[] getLasts() {
        return lasts;
    }
}
